﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CF_DAL_WS.Models;

namespace CF_DAL_WS.DTO
{
    public class FoodDTO
    {
        public int ID { get; set; }
        public int IDMenu { get; set; }
        public string Name { get; set; }
        public float Price { get; set; }
        public string Image { get; set; }
        public string ContentType { get; set; }
        public string SortIntroduction { get; set; }
        public List<OrderDetailDTO> OrderDetails { get; set; }

        public static FoodDTO Translate(Food Entity)
        {
            if (Entity != null)
            {
                var dto = new FoodDTO
                {
                    ID = Entity.ID,
                    Name = Entity.Name,
                    Image = Entity.Image,
                    ContentType = Entity.ContentType,
                    SortIntroduction = Entity.SortIntroduction,
                    OrderDetails = new List<OrderDetailDTO>()
                };
                if (Entity.Price != null)
                {
                    dto.Price = Entity.Price.Value;
                }
                if (Entity.IDMenu != null)
                {
                    dto.IDMenu = Entity.IDMenu.Value;
                }
                //if (Entity.OrderDetail != null)
                //{
                //    foreach (var item in Entity.OrderDetail)
                //    {
                //        dto.OrderDetails.Add(OrderDetailDTO.Translate(item));
                //    }
                //}
                return dto;
            }
            return null;
        }

        public static FoodDTO TranslateBase(Food Entity)
        {
            if (Entity != null)
            {
                var dto = new FoodDTO
                {
                    ID = Entity.ID,
                    Name = Entity.Name,
                    Image = Entity.Image,
                    SortIntroduction = Entity.SortIntroduction,
                    ContentType = Entity.ContentType
                };
                if (Entity.Price != null)
                {
                    dto.Price = Entity.Price.Value;
                }
                if (Entity.IDMenu != null)
                {
                    dto.IDMenu = Entity.IDMenu.Value;
                }
                return dto;
            }
            return null;
        }

        public static Food Reverse(FoodDTO obj)
        {
            var entity = new Food
            {
                Name = obj.Name,
                Image = obj.Image,
                SortIntroduction = obj.SortIntroduction,
                ContentType = obj.ContentType
            };
            if (obj.Price != -1)
            {
                entity.Price = obj.Price;
            }
            if (obj.IDMenu != -1)
            {
                entity.IDMenu = obj.IDMenu;
            }
            return entity;
        }

        public void UpdateEntity(ref Food entity)
        {
            entity.Name = Name;
            entity.Image = Image;
            entity.SortIntroduction = SortIntroduction;
            entity.ContentType = ContentType;
            if (Price != -1)
            {
                entity.Price = Price;
            }
            if (IDMenu != -1)
            {
                entity.IDMenu = IDMenu;
            }
        }
    }
}